package gui;

import bean.Book;
import bean.User;

import java.awt.*;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import service.BookService;
import service.BorrowedService;
import util.FrmUtil;
import util.StringUtil;

/**
 * 图书管理主界面.
 * 
 * @author 
 */
public class BookMgrPnl extends javax.swing.JPanel {
     private User user_login;
     private BookService bookService;
    /**
     * Creates new form BookMgrPnl
     */
     public void init(){
         if(!user_login.getType().equals(FrmUtil.MANAGER_USER)){
             btnBookAdd.setEnabled(false);
             btnBookMod.setEnabled(false);
             btnBookDel.setEnabled(false);
             
             cmbType.setEnabled(false);
             txtPrice.setEditable(false);
             txtAbstract.setEditable(false);
             txtMark.setEditable(false);
         }
         
     }
    
    public BookMgrPnl(User user) {
        user_login = user;
        bookService = new BookService();
        initComponents();
        init();
    }
    /**
     * 重置界面上部
     */
    public void resetFrmUp(){
        txtISBN.setText("");
        txtName.setText("");
        txtAuthor.setText("");
        txtPublisher.setText("");
        cmbType.setSelectedItem("计算机");
        txtPrice.setText("");
        txtMark.setText("");
        txtAbstract.setText("");
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtISBN = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtAuthor = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtPublisher = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        cmbType = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAbstract = new javax.swing.JTextArea();
        btnBookAdd = new javax.swing.JButton();
        btnBookMod = new javax.swing.JButton();
        btnBookDel = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtMark = new javax.swing.JTextField();
        btnBookQry = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblBooks = new javax.swing.JTable();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("更新图书"));

        jLabel1.setText("ISBN");

        txtISBN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtISBNActionPerformed(evt);
            }
        });
        txtISBN.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtISBNFocusLost(evt);
            }
        });

        jLabel2.setText("作 者");

        jLabel3.setText("图书分类");

        jLabel4.setText("书 名");

        jLabel5.setText("出版社");

        jLabel6.setText("价格");

        cmbType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "计算机", "艺术", "电子技术", "外文", "历史", "小说", "体育", " " }));

        txtAbstract.setColumns(20);
        txtAbstract.setRows(5);
        jScrollPane1.setViewportView(txtAbstract);

        btnBookAdd.setText("添加图书");
        btnBookAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBookAddActionPerformed(evt);
            }
        });

        btnBookMod.setText("修改图书");
        btnBookMod.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBookModActionPerformed(evt);
            }
        });

        btnBookDel.setText("删除图书");
        btnBookDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBookDelActionPerformed(evt);
            }
        });

        jLabel7.setText("图书摘要");

        jLabel8.setText("备 注");

        btnBookQry.setText("查询图书");
        btnBookQry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBookQryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtPublisher, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtISBN, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(39, 39, 39)
                        .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(106, 106, 106)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtMark)))
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(btnBookAdd)
                .addGap(35, 35, 35)
                .addComponent(btnBookMod)
                .addGap(37, 37, 37)
                .addComponent(btnBookDel)
                .addGap(34, 34, 34)
                .addComponent(btnBookQry)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtISBN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtPublisher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6)
                    .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtMark, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBookAdd)
                    .addComponent(btnBookMod)
                    .addComponent(btnBookDel)
                    .addComponent(btnBookQry))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("查询图书"));
        tblBooks.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ISBN", "书名", "作者", "出版社", "分类", "单价"
            }
        ));
        tblBooks.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblBooksMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblBooks);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 510, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnBookAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBookAddActionPerformed
        // 添加图书 图书编号不能重复
        String strId = txtISBN.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "图书ISBN不能为空！");
            txtISBN.requestFocus();
            return;
        }
        if(bookService.queryBookById(strId) != null){
            JOptionPane.showMessageDialog(this, "图书已经存在，请重新输入！");
            txtISBN.requestFocus();
            return;
        }
        String strName = txtName.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "书名不能为空！");
            txtName.requestFocus();
            return;
        }
        String strAuthor = txtAuthor.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "作者不能为空！");
            txtAuthor.requestFocus();
            return;
        }
        String strPublisher = txtPublisher.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "出版社不能为空！");
            txtPublisher.requestFocus();
            return;
        }
        String strPrice = txtPrice.getText().trim();
        
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "价格不能为空！");
            txtPrice.requestFocus();
            return;
        }
        Double dPrice = 0.0;
        try{
            dPrice = Double.parseDouble(strPrice);
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "价格非法！");
            txtPrice.requestFocus();
            return;
        }
        String strType = (String)cmbType.getSelectedItem();
        String strMark = txtMark.getText().trim();
        String strAbstract = txtAbstract.getText().trim();
        
        Book book = new Book();
        
        book.setId(strId);
        book.setName(strName);
        book.setAuthor(strAuthor);
        book.setPublisher(strPublisher);
        book.setPrice(dPrice);
        book.setMark(strMark);
        book.setBook_abstract(strAbstract);
        book.setType(strType);
        book.setStatus("0");//状态正常
        
        if (bookService.addBook(book)) {
            JOptionPane.showMessageDialog(this, "图书添加成功！");
            resetFrmUp();
        } else {
            JOptionPane.showMessageDialog(this, "图书添加失败，请联系管理员！");
        }
    }//GEN-LAST:event_btnBookAddActionPerformed

    private void btnBookModActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBookModActionPerformed
        // 修改图书资料
        String strId = txtISBN.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "图书ISBN不能为空！");
            txtISBN.requestFocus();
            return;
        }
        String strName = txtName.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "书名不能为空！");
            txtName.requestFocus();
            return;
        }
        String strAuthor = txtAuthor.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "作者不能为空！");
            txtAuthor.requestFocus();
            return;
        }
        String strPublisher = txtPublisher.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "出版社不能为空！");
            txtPublisher.requestFocus();
            return;
        }
        String strPrice = txtPrice.getText().trim();
        
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "价格不能为空！");
            txtPrice.requestFocus();
            return;
        }
        Double dPrice = 0.0;
        try{
            dPrice = Double.parseDouble(strPrice);
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "价格非法！");
            txtPrice.requestFocus();
            return;
        }
        String strType = (String)cmbType.getSelectedItem();
        String strMark = txtMark.getText().trim();
        String strAbstract = txtAbstract.getText().trim();
        
        Book book = bookService.queryBookById(strId);
        if(book == null){
            JOptionPane.showMessageDialog(this, "图书不存在，请检查！");
            txtISBN.requestFocus();
            return;
        }
        
        book.setName(strName);
        book.setAuthor(strAuthor);
        book.setPublisher(strPublisher);
        book.setPrice(dPrice);
        book.setMark(strMark);
        book.setBook_abstract(strAbstract);
        book.setType(strType);
        
        if (JOptionPane.showConfirmDialog(this, "确认修改？") == JOptionPane.OK_OPTION) {
            if (bookService.modBook(book)) {
                JOptionPane.showMessageDialog(this, "图书资料修改成功！");
                resetFrmUp();
            } else {
                JOptionPane.showMessageDialog(this, "图书资料修改失败，请联系管理员！");
            }
        }
    }//GEN-LAST:event_btnBookModActionPerformed

    private void btnBookDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBookDelActionPerformed
        //删除图书:已被借阅的图书不能删除
        String strId = txtISBN.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "图书ISBN不能为空！");
            txtISBN.requestFocus();
            return;
        }
        String strName = txtName.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "书名不能为空！");
            txtName.requestFocus();
            return;
        }
        String strAuthor = txtAuthor.getText().trim();
        if(!StringUtil.hasLength(strId)){
            JOptionPane.showMessageDialog(this, "作者不能为空！");
            txtAuthor.requestFocus();
            return;
        }
        Book book = bookService.queryBookById(strId);
        if(book == null){
            JOptionPane.showMessageDialog(this, "图书不存在，请检查！");
            txtISBN.requestFocus();
            return;
        }
        if(!strName.equals(book.getName()) || !strAuthor.equals(book.getAuthor())){
            JOptionPane.showMessageDialog(this, "图书信息有误，请检查！");
            txtISBN.requestFocus();
            return;
        }
        if (JOptionPane.showConfirmDialog(this, "确认删除？") == JOptionPane.OK_OPTION) {
            if (bookService.delBook(book)) {
                JOptionPane.showMessageDialog(this, "删除图书修改成功！");
                resetFrmUp();
            } else {
                if(new BorrowedService().queryBorrowedsOfBook(strId)!=null){
                    JOptionPane.showMessageDialog(this, "该图书已被借阅，不能删除！");
                    return;
                }
                JOptionPane.showMessageDialog(this, "删除图书修改失败，请联系管理员！");
            }
        }
    }//GEN-LAST:event_btnBookDelActionPerformed

    private void btnBookQryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBookQryActionPerformed
        // 按条件查询图书
        String strId = txtISBN.getText().trim();
        String strName = txtName.getText().trim();
        String strAuthor = txtAuthor.getText().trim();
        String strPublisher = txtPublisher.getText().trim();
        Book book = new Book();
        book.setId(strId);
        book.setName(strName);
        book.setAuthor(strAuthor);
        book.setPublisher(strPublisher);
        
        List<Book> books = bookService.queryAllBooksByWhere(book);
        showBooks(books);
        
    }//GEN-LAST:event_btnBookQryActionPerformed

    private void tblBooksMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblBooksMouseClicked
         // 单击表格回显选中的图书资料
        int select = tblBooks.getSelectedRow();
        if (select < 0) {
            return;
        }
        
         DefaultTableModel model = (DefaultTableModel)tblBooks.getModel();
//       Vector<Vector<String>> data = model.getDataVector();
         String strId = rowData[select][0];
         Book book = bookService.queryBookById(strId);
         showBook(book);
        
    }//GEN-LAST:event_tblBooksMouseClicked

    private void txtISBNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtISBNActionPerformed
        // TODO add your handling code here:
        opAfterEnterOrLost();
    }//GEN-LAST:event_txtISBNActionPerformed

    private void txtISBNFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtISBNFocusLost
        // TODO add your handling code here:
        opAfterEnterOrLost();
    }//GEN-LAST:event_txtISBNFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBookAdd;
    private javax.swing.JButton btnBookDel;
    private javax.swing.JButton btnBookMod;
    private javax.swing.JButton btnBookQry;
    private javax.swing.JComboBox cmbType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblBooks;
    private javax.swing.JTextArea txtAbstract;
    private javax.swing.JTextField txtAuthor;
    private javax.swing.JTextField txtISBN;
    private javax.swing.JTextField txtMark;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtPublisher;
    // End of variables declaration//GEN-END:variables

    public static String[][] rowData;
    /**
     * 把图书books显示在表格中.
     * 
     * @param books 
     */
    private void showBooks(List<Book> books) {
        String colums[] = {"ISBN","书名","作者","出版社","分类","单价"};
        
         rowData = new String[books.size()][colums.length];
        
        for(int i = 0; i < rowData.length; i++){
            Book book = books.get(i);
            rowData[i][0] = book.getId();
            rowData[i][1] = book.getName();
            rowData[i][2] = book.getAuthor();
            rowData[i][3] = book.getPublisher();
            rowData[i][4] = book.getType();
            rowData[i][5] = book.getPrice() + "";
        }
        DefaultTableModel model = new DefaultTableModel(rowData, colums){
            @Override//重写方法，使得表格不能被编辑
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };

        tblBooks.setRowHeight(30);
        Font font = new Font("宋体", Font.PLAIN, 16);
        tblBooks.setFont(font);
        tblBooks.setModel(model);
        
    }
    /**
     * 把图书book信息显示在界面上.
     * 
     * @param book 
     */
    private void showBook(Book book) {
        txtISBN.setText(book.getId());
        txtName.setText(book.getName());
        txtAuthor.setText(book.getAuthor());
        txtPublisher.setText(book.getPublisher());
        cmbType.setSelectedItem(book.getType());
        txtPrice.setText(book.getPrice() + "");
        txtMark.setText(book.getMark());
        txtAbstract.setText(book.getBook_abstract());
    }
    /**
     * 图书编号文本框回车或丢失焦点后的执行的操作.
     *
     * 查询图书是否存在，如果存在显示图书相关信息<br>
     */
    private void opAfterEnterOrLost() {
        String strId = txtISBN.getText().trim();
        if(!StringUtil.hasLength(strId)){
            return;
        }
        Book book = bookService.queryBookById(strId);
        if(book == null)
            return;
        showBook(book);
    }
}
